<template>
  <!-- 点击歌手进入的页面 -->
  <div class="music-list">
    <!-- 返回按钮 -->
    <div class="back" @click="goBack"><i class="icon-back"></i></div>
    <h1 class="title">{{ title }}</h1>
    <div class="bg-image" :style="bgImageStyle" ref="bgImage">
      <div class="play-btn-wrapper" @click="random" :style="playBtnStyle">
        <div v-show="songs.length" class="play-btn">
          <i class="icon-play"></i>
          <span class="text">随机播放全部</span>
        </div>
      </div>
      <div class="filter" :style="filterStyle"></div>
    </div>
    <Scroll class="list" :style="scrollStyle" v-loading='!this.songs.length' :probeType="3" @scroll="onScroll">
      <div class="song-list-wrapper">
        <SongList :songs="songs" @select="selectItem" :rank="rank"></SongList>
      </div>
    </Scroll>
  </div>
</template>
<script>
import Scroll from '@/components/base/scroll/scroll.vue'
import SongList from '@/components/base/song-list/song-list.vue'
import { mapActions, mapState } from 'vuex'
export default {
  name: 'MusicList',
  components: {
    Scroll,
    SongList
  },
  props: {
    songs: {
      type: Array,
      default: () => []
    },
    title: {
      type: String,
      default: ''
    },
    pic: {
      type: String,
      default: ''
    },
    rank: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      imgHeight: 0,
      scrollY: 0,
      maxTranslateY: 0
    }
  },
  computed: {
    // 解决btn因为滑到顶部导致padding-top为0而上浮
    playBtnStyle () {
      let display = ''
      if (this.scrollY >= this.maxTranslateY) {
        display = 'none'
      }
      return { display }
    },
    bgImageStyle () {
      const scrollY = this.scrollY
      let zIndex = 0
      let paddingTop = '70%'
      let height = 0
      let translateZ = 0
      if (scrollY > this.maxTranslateY) {
        zIndex = 10
        paddingTop = 0
        height = '40px'
        translateZ = 1
      }
      // 下拉图片缩放
      let scale = 1
      if (scrollY < 0) {
        scale = 1 + Math.abs(scrollY / this.imgHeight)
      }
      return {
        zIndex,
        paddingTop,
        height,
        backgroundImage: `url(${this.pic})`,
        transform: `scale(${scale}) translateZ(${translateZ}px)`
      }
    },
    filterStyle () {
      let blur = 0
      const scrollY = this.scrollY
      const imageHeight = this.imgHeight
      // 向上滑开始变模糊
      if (scrollY >= 0) {
        // 达到最大值后不变
        blur = Math.min(this.maxTranslateY / imageHeight, this.scrollY / imageHeight) * 10
      }
      return { backdropFilter: `blur(${blur}px)` }
    },
    scrollStyle () {
      const bottom = this.playList.length ? '1.7rem' : '0'
      return {
        top: `${this.imgHeight}px`,
        bottom: bottom
      }
    },
    ...mapState(['playList'])
  },
  mounted () {
    this.imgHeight = this.$refs.bgImage.clientHeight
    this.maxTranslateY = this.imgHeight - 40
  },
  methods: {
    goBack () {
      this.$router.back()
    },
    onScroll (pos) {
      this.scrollY = -pos.y
    },
    selectItem ({ song, index }) {
      this.sequencePlay({ list: this.songs, index: index })
    },
    random () {
      this.randomPlay(this.songs)
    },
    ...mapActions(['sequencePlay', 'randomPlay'])
  }
}
</script>
<style lang="scss" scoped>
.music-list {
  position: relative;
  height: 100%;
  .back {
    position: absolute;
    top: 0;
    left: .255864rem;
    z-index: 20;
    transform: translateZ(2px);
    .icon-back {
      display: block;
      padding: .426439rem;
      font-size: $font-size-large-x;
      color: $color-theme;
    }
  }
  .title {
    position: absolute;
    top: 0;
    left: 10%;
    width: 80%;
    z-index: 20;
    transform: translateZ(.085288rem);
    @include no-wrap();
    text-align: center;
    line-height: 1.705757rem;
    font-size: $font-size-large;
    color: $color-text;
  }
  .bg-image {
    position: relative;
    width: 100%;
    // transform-origin: top;
    background-size: cover;
    // padding-top: 70%;
    // height:0;
    .play-btn-wrapper {
        position: absolute;
        bottom: 20px;
        z-index: 10;
        width: 100%;
        .play-btn {
          box-sizing: border-box;
          width: 135px;
          padding: 7px 0;
          margin: 0 auto;
          text-align: center;
          border: 1px solid $color-theme;
          color: $color-theme;
          border-radius: 100px;
          font-size: 0;
        }
        .icon-play {
          display: inline-block;
          vertical-align: middle;
          margin-right: 6px;
          font-size: $font-size-medium-x;
        }
        .text {
          display: inline-block;
          vertical-align: middle;
          font-size: $font-size-small;
        }
      }
    .filter {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      background: rgba(7, 17, 27, 0.4);
    }
  }
  .list {
    position: absolute;
    bottom: 0;
    width: 100%;
    z-index: 0;
    // overflow: hidden;
    .song-list-wrapper {
      padding: .852878rem 1.279318rem;
      background: $color-background;
    }
  }
}
</style>
